package com.meihua.shellapp.activity.launch;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.MainActivity;
import com.meihua.shellapp.activity.login.GuideActivity;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.LoginBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.URL;

/**
 * 作者：lc on 2017/10/25.
 * 邮箱：lc0607live@163.com
 */

public class WelcomeActivity extends BaseActivity {

    private int type;//0，不可跳进下一页

    public synchronized void setType(int type) {
        this.type = type;
    }

    public synchronized int getType() {
        return type;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (SharedPreferencesUtil.getInstance().getBoolean(mContext, "isfirst_openApp", true)) {
                SharedPreferencesUtil.getInstance().saveBoolean(mContext, "isfirst_openApp", false);
                startActivity(new Intent(mContext, GuideActivity.class));
                finish();
            } else {
                if (getType() == 0) {
                    setType(1);
                    return;
                }
                if (MyApplication.getInstance().bean == null) {
                    startActivity(new Intent(mContext, MainActivity.class));
                    finish();
                } else {
                    startActivity(new Intent(mContext, MainActivity.class));
                    finish();
                }
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
        if (!"".equals(SharedPreferencesUtil.getInstance().getString(mContext, "passWord")) &&
                !"".equals(SharedPreferencesUtil.getInstance().getString(mContext, "phone"))) {
            login(SharedPreferencesUtil.getInstance().getString(mContext, "phone")
                    , SharedPreferencesUtil.getInstance().getString(mContext, "passWord"));
        } else {
            setType(1);
        }
    }

    /**
     * 登录
     */
    private void login(final String phone, final String pwd) {
//        startActivity(new Intent(mContext, MainActivity.class));
        OkGo.<String>post(URL.getUrl().LOGIN)
                .params("userName", phone)
                .params("passWord", pwd)
                .execute(new MyStringCallBack<LoginBean>(null) {
                    @Override
                    public void onSuccessData(LoginBean loginBean) {
                        super.onSuccessData(loginBean);
                        if (loginBean.getCode() == 1) {
                            SharedPreferencesUtil.getInstance().setObject(mContext, "user", loginBean.getData());
                            SharedPreferencesUtil.getInstance().saveString(mContext, "phone", phone);
                            SharedPreferencesUtil.getInstance().saveString(mContext, "passWord", pwd);
                            SharedPreferencesUtil.getInstance().saveString(mContext, Constant.PAY_PASSWARD, loginBean.getData().getIspay_pwd());
                            MyApplication.getInstance().bean = loginBean.getData();
                            MyApplication.getInstance().bean.setPhone(phone);
//                            ToastUtils.getInstance().showToast("自动登录成功");
                            if (getType() == 0) {
                                return;
                            }
                            startActivity(new Intent(mContext, MainActivity.class));
                            finish();
                        } else {
//                            ToastUtils.getInstance().showToast("自动登录失败");
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        setType(1);
                    }
                });

//
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);//启动页停滞时间
                    handler.sendEmptyMessage(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
